import { createSlice } from "@reduxjs/toolkit";

const countSlice = createSlice({
    //创建一个命名空间
    name: 'counter',
    //初始化一个值
    initialState: {
        count: 0,
    },
    //创建reducer们
    reducers: {
        //reduce中处理函数接收两个参数 分别是当前reducer中上一次的值\本次dispacth的action对象
        //action对象中有一个payload就是用户调用action函数时传递的参数
        increment(prev, { payload }) {
            prev.count += payload
        },
        decrement(prev, action) {
            prev.count -= action.payload
        },
        ifOddIncrement(prev, action) {
            if (prev % 2 !== 0) {
                prev.count += action.payload
            }
        },
        waitIncrement(prev, action) {
            prev.count += action.payload
        },

    }
})
//将返回的countSlice上的reducer暴露
export default countSlice.reducer

//将返回的countSlice上的actions暴露
export const { increment, decrement, ifOddIncrement, waitIncrement } = countSlice.actions